package com.exartisan.adapter;

import java.util.ArrayList;

import com.exartisan.bean.FbComment;
import com.exartisan.network.NetworkUtility;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.DateTimeFormat;
import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CommentsAdapter extends BaseAdapter {

	ArrayList<FbComment> comments;
	Context ctx;

	public CommentsAdapter(ArrayList<FbComment> comments, Context ctx) {
		super();
		this.comments = comments;
		this.ctx = ctx;
	}

	@Override
	public int getCount() {
		return comments.size();
	}

	@Override
	public Object getItem(int position) {
		return comments.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listView;
		ViewHolder holder;

		listView = new View(ctx);

		if (convertView == null) {
			holder = new ViewHolder();
			listView = inflater.inflate(R.layout.event_detail_comments, null);
			holder.imgAvatar = (SmartImageView) listView
					.findViewById(R.id.img_avataComment);
			holder.tvUserName = (TextView) listView
					.findViewById(R.id.tv_userNameComment);
			holder.tvCreatedTime = (TextView) listView
					.findViewById(R.id.tv_dateComment);
			holder.tvContent = (TextView) listView
					.findViewById(R.id.tv_commentForum);
			listView.setTag(holder);
		} else {
			listView = (View) convertView;
			holder = (ViewHolder) listView.getTag();
		}
		
		FbComment c = comments.get(position);
		
		holder.tvUserName.setText(c.getFbUser().getName());
		holder.tvContent.setText(c.getMessage());
		holder.tvCreatedTime.setText(DateTimeFormat.format(c.getCreated_time(), DateTimeFormat.SHORT_VALUE));
		
		//holder.imgAvatar.setImageUrl(NetworkUtility.GRAPH_API_URL + "/" + c.getFbUser().getId() + "/picture?width=100&height=100");
		
		ImageLoader.getInstance().displayImage(NetworkUtility.GRAPH_API_URL + "/" + c.getFbUser().getId() + "/picture?width=100&height=100", holder.imgAvatar);
		
		return listView;
	}

	private class ViewHolder {
		TextView tvUserName, tvCreatedTime, tvContent;
		SmartImageView imgAvatar;
	}

}
